package com.starry.core.ext

import android.view.View
import com.starry.core.R

/**
 * Package:com.starry.dotpen_patients.utils
 * Author:starr
 * Time:2024/3/18 0:13
 * Description:
 */
private var <T : View>T.triggerLastTime: Long
    get() = if (getTag(R.id.triggerLastTimeKey) != null) getTag(R.id.triggerLastTimeKey) as Long else 0
    set(value) {
        setTag(R.id.triggerLastTimeKey, value)
    }

/**
 * 给view添加一个延迟的属性（用来屏蔽连击操作）
 */
private var <T : View> T.triggerDelay: Long
    get() = if (getTag(R.id.triggerDelayKey) != null) getTag(R.id.triggerDelayKey) as Long else -1
    set(value) {
        setTag(R.id.triggerDelayKey, value)
    }

fun View.clickAble(): Boolean {
    var clickable = false
    val currentClickTime = System.currentTimeMillis()
    if (currentClickTime - triggerLastTime >= triggerDelay) {
        triggerLastTime = currentClickTime
        clickable = true
    }

    return clickable
}

//设置一个防抖的点击事件
fun <T : View> T.singleClick(delay: Long = 1000L, block: (T) -> Unit) {
    triggerDelay = delay
    setOnClickListener {
        if (clickAble()) {
            block(this)
        }
    }
}